#! /usr/local/bin/python

# read file
import sys
import os
import shutil

openfile = open( sys.argv[ 1 ] + "course.data", 'r' )
content = openfile.readlines()

courseFile = []
tableOfContents = []
courseName = ""

######################################
# read the course file into an array
######################################
for newCourseHash in content:
	# take a line
	courseLine = newCourseHash.strip()

	if courseLine == '':
		continue
	
	if courseLine.find( '>>>' ) <> -1:
		courseName = courseLine
		#print courseName	
	else:
		courseFile.append( courseLine )
		
openfile.close()


courseSegments= []
# iterate over the lines and process the line into course objects
for index in range( len( courseFile ) ):
	# hash marks determine when there's a new course segment
	if courseFile[ index ].find( '###' ) <> -1:
		courseSegment = []
		
		# move to next line
		index = index + 1
		
		while courseFile[ index ].find( '###' ) == -1:
			courseSegmentLine = [] 
			
			##### title
			if courseFile[ index ].find( 'title:' ) <> -1:
				value = courseFile[ index ].split( ':' )[ 1 ]
				# add to the table of contents
				tableOfContents.append( value )
				courseSegmentLine = [ 'title', value ]
				courseSegment.append( courseSegmentLine )
				
			##### type
			elif courseFile[ index ].find( 'type:' ) <> -1:
				value = courseFile[ index ].split( ':' )[ 1 ]
				courseSegmentLine = [ 'type', value ]
				courseSegment.append( courseSegmentLine )
			
			##### type
			elif courseFile[ index ].find( 'source:' ) <> -1:
				value = courseFile[ index ].split( ':' )[ 1 ]
				courseSegmentLine = [ 'source', value ]
				courseSegment.append( courseSegmentLine )
				
			##### audio
			elif courseFile[ index ].find( 'audio:' ) <> -1:
				value = courseFile[ index ].split( ':' )[ 1 ]
				courseSegmentLine = [ 'audio', value ]
				courseSegment.append( courseSegmentLine )
				
			##### links, URL must not have http: (with the colon) because of the split below
			elif courseFile[ index ].find( 'link:' ) <> -1:
				value = courseFile[ index ].split( ':' )[ 1 ]
				# split the comma into URL and link text
				linkvalue = value.split( ',' )
				courseSegmentLine = [ 'link', linkvalue[ 0 ], linkvalue[ 1 ] ]
				courseSegment.append( courseSegmentLine )
				
			##### resources, URL must not have http: (with the colon) because of the split below
			elif courseFile[ index ].find( 'document:' ) <> -1:
				value = courseFile[ index ].split( ':' )[ 1 ]
				# split the comma into URL and resource name
				resourcevalue = value.split( ',' )
				courseSegmentLine = [ 'resource', resourcevalue[ 0 ], resourcevalue[ 1 ] ]
				courseSegment.append( courseSegmentLine )
				
			##### script
			elif courseFile[ index ].find( 'script:' ) <> -1:
				value = courseFile[ index ].split( ':' )[ 1 ]
				courseSegmentLine = [ 'script', value ]
				courseSegment.append( courseSegmentLine )
				
			index = index + 1
			
			# keep from going out of range on the index
			if index == len( courseFile ):
				break
		courseSegments.append( courseSegment )





def processQuizData( file ):
	openfile = open( file, 'r' )
	content = openfile.readlines()
	
	quiz = []
	quizQuestion = []
	
	for questionItem in content:
		# take a line
		questionLine = questionItem.strip()
	
		if questionLine == '':
			continue
	
		# if the line is a question designated by the hash
		# take the other lines.  at this point, questionLine
		# is the formatted question
		if questionLine.find( '#' ) <> -1:
			quizQuestion = []
			quizQuestion.append( questionLine.strip( '#' ) )
		else:
			quizQuestion.append( questionLine )
		
		if questionLine.find( '#' ) <> -1:
			quiz.append( quizQuestion )
	
	openfile.close()
	
	###########################
	# from here, we have the quiz object. each item in the list
	# has 2+ items with the first items as the quiz question
	# and the remaining items as answers	
	
	theQuiz = []
	
	for qObject in quiz:
		quizItem = []
		counter = 1
		for qItem in qObject:
			if counter == 1:
				quizItem.append( qItem )
			if counter > 1:
				# choice object: 1 - answer, 2 - correct
				oChoice = []
				if qItem.find( '^' ) <> -1:
					oChoice.append( qItem.strip( '^' ) );
					oChoice.append( 'true' );
				else:
					oChoice.append( qItem );
					oChoice.append( 'false' );
				quizItem.append( oChoice )
			counter = counter + 1
	
		theQuiz.append( quizItem )
	
	quizContent = ""
	quizContent = quizContent + "<form action='javascript:processQuiz()' name='quiz'>"
	quizContent = quizContent + "<ol>" 
	counter = 0
	
	for qItem in theQuiz:
		quizContent = quizContent + "<li><p class='question-title' id='question-title" + str( counter ) + "'>" + qItem[ 0 ] + "</p>\n"
		qName = "q" + str( counter )
		
		answerCounter = 0
		for qAnswer in qItem:
			if answerCounter == 0:
				answerCounter = answerCounter + 1
				continue
			else:
				quizContent = quizContent + "<input name='" + qName + "' type='radio'"
				if qAnswer[ 1 ] == "true":
					quizContent = quizContent + "value='1'/>\n"
				else:
					quizContent = quizContent + "value='0'/>\n"
				quizContent = quizContent + qAnswer[ 0 ] + "<br/>\n"
				answerCounter = answerCounter + 1
		quizContent = quizContent + "<br/>\n"
		counter = counter + 1
	
	quizContent = quizContent + "</ul>"
	quizContent = quizContent + "<br/><input type='submit' name='submit' value='Submit Your Answers'/>\n"
	quizContent = quizContent + "<input type='reset' name='reset' value='Clear Answers'/>\n"
	quizContent = quizContent + "</form>"

	return quizContent

















if os.path.isdir( sys.argv[ 2 ] ) <> 1:
	os.mkdir( sys.argv[ 2 ] )

##################################
# create a page for each segment
##################################

def getSlide( src ):
	return "<article id='content'><img src='" + src + "' width='600' height='450'/></article>"
	
def getQuiz():
	return "<article id='content'><table width='600'><tr><td align='left'>" + processQuizData( sys.argv[ 1 ] + "/quiz.data" ) + "</td></tr></table></article>"

def getVideo( src ):
	vid = src.split( ',' )
	width = "600"
	height = "400"
	if len( vid ) > 2:
		width = vid[ 1 ]
		height = vid[ 2 ]
	
	ht = "<article id='content'><img src='graphics/header.jpg'><br/><table border='0' width='600'><tr><td align='center'><iframe width='" + width + "' height='" + height + "' src='http://" + vid[ 0 ] + "' frameborder='0' allowfullscreen></iframe></td></tr>"
	ht = ht + "<tr><td><p id='video-disclaimer'>This video is used for educational purposes. "
	ht = ht + "The views expressed in this video are those of its participants and do not necessarily reflect the views of the US Department of Education, "
	ht = ht + "The Academy of Teacher Excellence, nor the University of Texas at San Antonio. </p> </td></tr></table></article>"
	return ht
	
def getAudio( src ):
	line1 = "<aside id='audio'><p class='resource-title'>Audio</p><audio id='audio' controls='none' autoplay='autoplay'>"
	line2 = "<source src='" + src + ".mp3' type='audio/mp3' />"
	line3 = "<source src='" + src + ".ogv' type='audio/ogg' />"
	line4 = "Your browser does not support the audio tag.</audio><br/><br/></aside>"
	return line1 + line2 + line3 + line4
	
def getScript( src ):
	return "<section id='script'><p class='resource-title'>Script</p><p id='script-text'>" + src + "</p></section>"

def getResources( src ):
	lineStart = "<aside id='docs'><p class='resource-title'>Resources</p><ul class='document-links'>"
	lines = ""
	for item in src:
		lines = lines + "<li><a class='resource-link' target='_blank' href='http://" + item[ 0 ] + "'>" + item[ 1 ] + "</a>"
	lineEnd = "</ul><br/></aside>" 
	return lineStart + lines + lineEnd

def getLinks( src ):
	lineStart = "<aside id='links'><p class='resource-title'>Links</p><ul class='document-links'>"
	lines = ""
	for item in src:
		lines = lines + "<li><a class='resource-link' href='http://" + item[ 0 ] + "' target='_blank'>" + item[ 1 ] + "</a>"
	lineEnd = "</ul><br/></aside>" 
	return lineStart + lines + lineEnd

def getQuiz( src ):
	quizLines = "<div><img src='graphics/header.jpg' alt='ATE I3 Header'/><br/>"
	openQuiz = open( src, 'r' )
	content = openQuiz.readlines()

	# read start file
	for eachLine in content:
		quizLines = quizLines + eachLine

	openQuiz.close()

	quizLines = quizLines + "</div>"
	return quizLines

def getBox():
	return "<aside id='boxaside'><img src='graphics/box.jpg' id='box'/></aside>";







pageStart = ""
openPageStart = open( 'templates/page-start.html', 'r' )
pageStartContent = openPageStart.readlines()
for eachLine in pageStartContent:
	pageStart = pageStart + eachLine

pageEnd = ""
openPageEnd = open( 'templates/page-end.html', 'r' )
pageEndContent = openPageEnd.readlines()
for eachLine in pageEndContent:
	pageEnd = pageEnd + eachLine

contentSection = ""
audio = ""
type = ""
script = ""	
content = ""
source = ""
resources = []
links = []
# create the file

for index in range( len( courseSegments ) ):
	openPageFile = open( sys.argv[ 2 ] + '/page' + str( index ) + '.html', 'w' )		
	
	resources = []
	links = []
	# go through each item in the course segment
	for courseItem in courseSegments[ index ]:
	
		if courseItem[ 0 ] == 'script':
			script = getScript( courseItem[ 1 ] )
			
		if courseItem[ 0 ] == 'type':
			type = courseItem[ 1 ]
			
		if courseItem[ 0 ] == 'source':
			source = courseItem[ 1 ]
			
		if courseItem[ 0 ] == 'audio':
			audio = getAudio( courseItem[ 1 ] )
			
		if courseItem[ 0 ] == 'resource':
			resources.append( [ courseItem[ 1 ], courseItem[ 2 ] ] )
			
		if courseItem[ 0 ] == 'link':
			links.append( [ courseItem[ 1 ], courseItem[ 2 ] ] )
			
	contentSection = ""
	
	if type == "slide":
		contentSection = contentSection + getSlide( source )
	elif type == "video":
		contentSection = contentSection + getVideo( source )
	elif type == "quiz":
		contentSection = contentSection + getQuiz( source )
		
	
	contentSection = contentSection + "<article id='resources'>"
	contentSection = contentSection + audio
	if len( resources ) > 0:
		contentSection = contentSection + getResources( resources )
	if len( links ) > 0:
		contentSection = contentSection + getLinks( links )
		
	contentSection = contentSection + getBox()
	
	contentSection = contentSection + "</article></section>" + script
	
	openPageFile.write( pageStart + contentSection + pageEnd )
	
	
	
	
	
	

#############################
# copy header.html (start and end) over
#############################


varname = "<span id='course-title'>" + courseName.strip( '>' ) + "</span>"

####### create Navigation file
openHeaderFile = open( sys.argv[ 2 ] + '/header.html', 'w' )		
# open templates
openHeaderFileStart = open( 'templates/header-start.html', 'r' )
content = openHeaderFileStart.readlines()

# read start file
for eachLine in content:
	openHeaderFile.write( eachLine )

openHeaderFileStart.close()

# write the extra line with the course name
openHeaderFile.write( varname )

openHeaderFileEnd = open( 'templates/header-end.html', 'r' )
content = openHeaderFileEnd.readlines()

# read end file
for eachLine in content:
	openHeaderFile.write( eachLine )
	
openHeaderFileEnd.close()
openHeaderFile.close()



	
	
	
	
	
	
	
		
	
	
#############################
# create navigation file
#############################

####### create Navigation file
openNavFile = open( sys.argv[ 2 ] + '/navigation.html', 'w' )		
# open templates
openNavTemplateStart = open( 'templates/navigation-start.html', 'r' )
content = openNavTemplateStart.readlines()

# read start file
for eachLine in content:
	openNavFile.write( eachLine )

openNavTemplateStart.close()

openNavTemplateEnd = open( 'templates/navigation-end.html', 'r' )
content = openNavTemplateEnd.readlines()

#### create the new navs
# each html page is labeld pageN.html
for index in range( len( tableOfContents ) ):
	value = str( index )
	openNavFile.write( "<p class='navigation-link-bg' id='page" + value + "'><a href='page" + value + ".html' class='navigation-link' onclick='parent.gotoPage(" + value + ")' target='content'>" + tableOfContents[ index ] + "</a></p>" )

# read end file
for eachLine in content:
	openNavFile.write( eachLine )
openNavFile.write( "<p class='navigation-link-bottom'></p>" )
openNavTemplateEnd.close()

openNavFile.close()





#############################
# copy navigation.js over
#############################

if os.path.isdir( sys.argv[ 2 ] + "/scripts" ) <> 1:
	os.mkdir( sys.argv[ 2 ] + "/scripts" )
	
vardecl = "var MAX_SLIDES = " + str( len( courseSegments ) ) + ";"

jsfile = ""
openJS = open( 'templates/navigation_scripts.js', 'r' )
jsContent = openJS.readlines()
for eachLine in jsContent:
	jsfile = jsfile + eachLine
jsfile = vardecl + jsfile

openJSFile = open( sys.argv[ 2 ] + '/scripts/navigation_scripts.js', 'w' )		
openJSFile.write( jsfile )
openJSFile.close()







#############################
# copy scorm_scripts.js over
#############################


if os.path.isdir( sys.argv[ 2 ] + "/scripts" ) <> 1:
	os.mkdir( sys.argv[ 2 ] + "/scripts" )
	
vardecl = "function getCourseName() { return \"" + courseName.strip( '>>>' ) + "\"; }\n"

#shutil.copyfile( 'templates/scorm_scripts.js', sys.argv[ 2 ] + "/scripts/scorm_scripts.js" )

jsfile = ""
openJS = open( 'templates/scorm_scripts.js', 'r' )
jsContent = openJS.readlines()
for eachLine in jsContent:
	jsfile = jsfile + eachLine
jsfile = vardecl + jsfile

openJSFile = open( sys.argv[ 2 ] + '/scripts/scorm_scripts.js', 'w' )		
openJSFile.write( jsfile )
openJSFile.close()










#############################
# copy template files over
#############################
	
if os.path.isdir( sys.argv[ 2 ] + "/styles" ) <> 1:
	os.mkdir( sys.argv[ 2 ] + "/styles" )
	
if os.path.isdir( sys.argv[ 2 ] + "/slides" ) <> 1:
	os.mkdir( sys.argv[ 2 ] + "/slides" )
	
if os.path.isdir( sys.argv[ 2 ] + "/audio" ) <> 1:
	os.mkdir( sys.argv[ 2 ] + "/audio" )
	
if os.path.isdir( sys.argv[ 2 ] + "/graphics" ) <> 1:
	os.mkdir( sys.argv[ 2 ] + "/graphics" )
	
shutil.copyfile( 'templates/header_styles.css', sys.argv[ 2 ] + "/styles/header_styles.css" )
shutil.copyfile( 'templates/navigation_styles.css', sys.argv[ 2 ] + "/styles/navigation_styles.css" )
shutil.copyfile( 'templates/styles.css', sys.argv[ 2 ] + "/styles/styles.css" )
shutil.copyfile( 'templates/index.html', sys.argv[ 2 ] + "/index.html" )
shutil.copyfile( 'templates/quiz.js', sys.argv[ 2 ] + "/scripts/quiz.js" )
shutil.copyfile( 'templates/SCORM_API_wrapper.js', sys.argv[ 2 ] + "/scripts/SCORM_API_wrapper.js" )
shutil.copyfile( 'templates/page-scripts.js', sys.argv[ 2 ] + "/scripts/page-scripts.js" )
shutil.copyfile( 'graphics/header.jpg', sys.argv[ 2 ] + "/graphics/header.jpg" )




	